package services

import (
	"github.com/labstack/gommon/log"
	"law_case_api/dto"
	"law_case_api/models"
)

// CreateCustomerAccessory 创建客户附件信息
func (customer CustomerService) CreateCustomerAccessory(wzjCustomerAccessory models.WzjCustomerAccessory) (bool, error) {
	if err := db.Create(&wzjCustomerAccessory).Error; err != nil {
		log.Error("CreateCustomerAccessory--创建客户附件信息失败,失败原因：", err)
		return false, err
	}
	return true, nil
}

// DelCustomerAccessory 删除客户附件
func (customer CustomerService) DelCustomerAccessory(wzjCustomerAccessory models.WzjCustomerAccessory) (bool, error) {
	if err := db.Where("accessory_id=?", wzjCustomerAccessory.AccessoryId).Delete(&wzjCustomerAccessory).Error; err != nil {
		log.Error("DelCustomerAccessory--删除客户附件失败,失败原因：", err)
		return false, err
	}
	return true, nil
}

// GetCustomerAccessoryListByCustomerId  根据合同信获取附件列表
func (customer CustomerService) GetCustomerAccessoryListByCustomerId(customerId int) ([]dto.CustomerAccessoryResp, error) {
	var wzjCustomerAccessoryList []dto.CustomerAccessoryResp
	sql := `SELECT b.id file_id,b.file_name,b.file_path FROM wzj_db.wzj_customer_accessory a
		INNER JOIN wzj_db.wzj_file b ON a.file_id = b.id 
		WHERE a.customer_id = ?;`
	if err := db.Raw(sql, customerId).
		Find(&wzjCustomerAccessoryList).Error; err != nil {
		log.Error("GetCustomerAccessoryListByContractId--查询失败,失败原因：", err)
		return wzjCustomerAccessoryList, err
	}
	return wzjCustomerAccessoryList, nil
}
